package com.qingxin.park.service;
import java.util.Date;
import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.qingxin.park.entity.Provenance;
import com.qingxin.sys.dao.Querier;
import com.qingxin.sys.dao.Updater;
import com.qingxin.sys.entity.LoginUser;
import com.qingxin.sys.entity.MyUploadFileParam;
import com.qingxin.sys.mvc.PageParam;
import com.qingxin.sys.mvc.Pager;
import com.qingxin.sys.mvc.SysError;
import com.qingxin.sys.service.BaseService;
import com.qingxin.sys.support.SystemCfg;
import com.qingxin.sys.support.UploadFile;
import com.qingxin.sys.utils.BeanUtil;
import com.qingxin.sys.utils.StringUtils;

/**
 * 园区地块
 * @author Administrator
 *
 */

@Service
public class ProvenanceService extends BaseService<Provenance, String> {
	
	@Autowired
	private ProvenanceImgsService provenanceImgsService;

	/**
	 * 获取列表数据
	 * @param parkid
	 * @param blockName
	 * @param isuse
	 * @param pageParam
	 * @param loginUser
	 * @return
	 */
	public Pager list(String parkid,String datatype,String proName, String subName, PageParam pageParam, LoginUser loginUser){
		Querier querier = Querier.create("select new com.qingxin.park.entity.Provenance(" +
				"p.proid, p.proName, p.subtime,p.subName)" +
				" from Provenance p where 1=1");
		if(datatype.equals("GROUP")){
			String groupid = null;
			if(!SystemCfg.isAdminUser(loginUser) && !SystemCfg.isSystemAdminUser(loginUser)){
				groupid = loginUser.getGroupid();
			}
			
			if(StringUtils.isNotEmpty(groupid) && !"0".equals(groupid)){
				querier.append(" and p.parkid in (select distinct v.parkid from FarmPark v where v.groupid like '"+groupid+"%')");
			}
			
			if(!SystemCfg.isDataAdminUser(loginUser)){
				querier.append(" and p.parkid in (select distinct v.farmPark.parkid from SysUserParkRel v where v.sysUser.userid='"+loginUser.getUserid()+"')");
			}
			
		}else {
			querier.append(" and p.parkid=:parkid ");
			querier.setParam("parkid", parkid);
		}
		
		if(StringUtils.isNotEmpty(proName)){
			proName = proName.replace("'", "");
			querier.append(" and (p.proName like :proName ) ");
			querier.setParam("proName", "%"+proName+"%");
		}
		
		querier.append(pageParam.getOrderby(" order by p.proName asc"));
		Pager pager = pageByQuerier(querier, pageParam);
		
		return pager;
		
	}
	
	
	
	/**
	 * 获取园区菜单树数据
	 * @param loginUser
	 * @param isParent
	 * @return
	 */
/*	@SuppressWarnings("unchecked")
	public List<TreeNode> getUITreeData(LoginUser loginUser){
		List<TreeNode> list = new ArrayList<TreeNode>();
		StringBuilder hql = new StringBuilder();
		hql.append("select new com.qingxin.park.entity.FarmPark(parkid,parkName)");
		hql.append(" from FarmPark");
		List<FarmPark> parks = listByHql(hql.toString());
		if(parks != null){
			for(FarmPark park : parks){
				list.add(new TreeNode(park.getParkid(),park.getParkName(),"PROVENANCE",null,false));
			}
		}
		return list;
	}*/
	
	/**
	 * 添加产地环境信息
	 * @param block
	 * @param parkid
	 * @param loginUser
	 * @param request
	 * @return
	 */
	/**
	 * 添加产地环境信息
	 * @param proRecord
	 * @param plantid
	 * @param loginUser
	 * @param request
	 * @return
	 */
	public SysError add(Provenance provenance, MyUploadFileParam uploadFileParam,LoginUser loginUser, HttpServletRequest request){
		
		
		Date now = new Date();
		provenance.setSubtime(now);
		
		
		if(StringUtils.isNotEmpty(provenance.getSubAttach())){
			UploadFile.moveTmpTo(provenance.getSubAttach(),UploadFile.DIR_PRO_ATTACHMENT);
		}
		
		this.save(provenance);
		
		provenanceImgsService.add(provenance.getProid(),uploadFileParam, loginUser);
		
		return null;
	}
	
	
	/**
	 * 修改地块信息
	 * @param block
	 * @param loginUser
	 * @param request
	 * @return
	 */
	public SysError update(Provenance provenance, MyUploadFileParam uploadFileParam, LoginUser loginUser, HttpServletRequest request){
		Provenance old = getSafeEntity(provenance.getProid());
		if(old == null){
			return new SysError("参数错误");
		}
		/*if(isNameDuplicate(provenance.getProName(), provenance.getProid(), old.getProid())){
			return new SysError("该名称已存在");
		}*/
		if(StringUtils.isNotEmpty(provenance.getSubAttach()) && !old.getSubAttach().equals(provenance.getSubAttach())){
			UploadFile.moveTmpTo(provenance.getSubAttach(),UploadFile.DIR_PRO_ATTACHMENT);
		}
		
		String fields = "proName,subtime,remark,subName,subAttach";
		BeanUtil.setTargetFields(old, provenance, fields, true);
		Updater<Provenance> updater = new Updater<>(old, fields);
		this.update(updater);
		provenanceImgsService.add(provenance.getProid(),uploadFileParam, loginUser);
		return null;
		
	}
	
	
	/**
	 * 删除产地环境信息
	 * @param blockids
	 * @param loginUser
	 * @param request
	 * @return
	 */
	public Object delete(String[] proids,MyUploadFileParam uploadFileParam, LoginUser loginUser, HttpServletRequest request){
		for(String proid : proids){
			Provenance provenance = get(proid);
			if(provenance == null){
				continue;
			}
			this.delete(provenance);
			provenanceImgsService.add(provenance.getProid(),uploadFileParam, loginUser);
		}
		return null;
	}
	
	
	/**
	 * 判断指定名称是否被使用
	 * @param blockName
	 * @param blockid
	 * @param parkid
	 * @return
	 */
	public boolean isNameDuplicate(String proName, String proid, String parkid){
		if(StringUtils.isEmpty(proid)){
			Object obj = getByHql("select proid from Provenance where parkid=? and proName=?", parkid, proName);
			return obj!=null;
		}else {
			Object obj = getByHql("select proid from Provenance where parkid=? and proName=? and proid<>?", parkid, proName, proid);
			return obj!=null;
		}
	}

}
